# coding: utf8

import os
import logging
import requests_html
import traceback
import re
from multiprocessing.pool import ThreadPool


"""南开大学
电子信息与光学工程学院, 微电子工程系+IC设计中心
"""

logging.basicConfig(level=logging.INFO)


session = requests_html.HTMLSession()
pool = ThreadPool(os.cpu_count() * 2)


reduce_breaklines = re.compile(r'\n+')
sex_ptn = re.compile(r'性别[:：][\s]*([男|女])[\s]*')
dep_ptn = re.compile(r'所属部门[:：]\s*(\S*)')
pos_ptn = re.compile(r'职称[:：]\s*(\S*)')
degree_ptn = re.compile(r'学历[:：]\s*(\S*)')
aff_ptn = re.compile(r'行政职务[:：]\s*(\S*)')
subj_ptn = re.compile(r'所学专业[:：]\s*(\S*)')
tel_ptn = re.compile(r'办公电话[:：]\s*([0-9\-+\s]*)')
email_ptn = re.compile(r'邮箱[:：]\s*(\S+@(\S+\.)+\S+)')
ri_ptn = re.compile(r'研究方向[:：]\s*([\s\S]*?\n)')


def teacher_list():
    try:
        # 微电子工程系
        urls = ["https://ceo.nankai.edu.cn/szll/wdzgcx.htm", "https://ceo.nankai.edu.cn/szll/ICsjzx.htm"]
        teachers = []
        for _url in urls:
            r = session.get(_url)
            if r.status_code == 200:
                trs = r.html.xpath("//table[contains(@class, 'publicClassTable')][1]/tbody[1]/tr")

                for tr in trs:
                    tds = tr.xpath("//td")
                    assert len(tds) == 4, "tds len not 4, but %d" % len(tds)
                    name = tds[0].full_text
                    url = list(tds[0].absolute_links)[0]
                    pos = tds[1].full_text
                    dep = tds[2].full_text
                    ri = tds[3].full_text

                    logging.info("name: %s, pos: %s, dep: %s, ri: %s, url: %s", name, pos, dep, ri, url)

                    teachers.append((name, url))
                pass
            else:
                logging.warning("status code error: %s", r.status_code)
                r.raise_for_status()
            pass  # end for
        pool.imap_unordered(teacher_detail, teachers)
    except Exception as e:
        traceback.print_exc()
        raise e


def teacher_detail(payload):
    name, url = payload
    logging.info("load %s ...", name)

    try:
        r = session.get(url)
        if r.status_code == 200:
            basic_info = r.html.xpath("//div[@class='teacherList'][1]")[0]
            avatar = basic_info.xpath("//div[@class='tchListLeftN'][1]//img/@src")
            avatar = "https://ceo.nankai.edu.cn" + avatar[0] if avatar else ""

            name = basic_info.xpath("//div[@class='tchListRightN'][1]/div[@class='tchLstRightTop'][1]/h5[1]")[0].full_text

            # others: str = basic_info.xpath("//div[@class='tchListRightN'][1]")[0].full_text
            # sex, dep, pos, degree, affi, subj, tel, email, ri = [ptn.search(others) for ptn in (sex_ptn, dep_ptn, pos_ptn, degree_ptn, aff_ptn, subj_ptn, tel_ptn, email_ptn, ri_ptn)]
            # sex, dep, pos, degree, affi, subj, tel, email, ri = [(match.group(1).strip() if match else '') for match in (sex, dep, pos, degree, affi, subj, tel, email, ri)]

            others = basic_info.xpath("//div[@class='tchListRightN'][1]//div[@class='tchLstRightCnt']")
            assert len(others) == 6
            sex = others[0].xpath('//span[3]')[0].full_text.strip()
            dep = others[0].xpath('//span[6]')[0].full_text.strip()
            pos = others[1].xpath('//span[3]')[0].full_text.strip()
            degree = others[1].xpath('//span[6]')[0].full_text.strip()
            affi = others[2].xpath('//span[3]')[0].full_text.strip()
            subj = others[2].xpath('//span[6]')[0].full_text.strip()
            tel = others[3].xpath('//span[3]')[0].full_text.strip()
            email = others[4].xpath('//span[3]')[0].full_text.strip()
            ri = others[5].xpath('//span[3]')[0].full_text.strip()

            logging.info("name: %s, sex: %s, dep: %s, pos: %s, degree: %s, affi: %s, subj: %s, tel: %s, email: %s, ri: %s, avatar: %s", name, sex, dep, pos, degree, affi, subj, tel, email, ri, avatar)

            pass
        else:
            logging.warning("status code error: %s", r.status_code)
            r.raise_for_status()

    except Exception as e:
        traceback.print_exc()
        raise e


if __name__ == '__main__':
    teacher_list()

    pool.close()
    pool.join()

    session.close()
    pass

